package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsProjectReagentCode;
import com.ruoyi.project.product.domain.InsProjectReagentCodeDTO;
import com.ruoyi.project.product.service.IInsProjectReagentCodeService;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 产品试剂代码Controller
 *
 * @author taney
 * @date 2020-12-15
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/projectReagentCode")
public class InsProjectReagentCodeController extends BaseController {

    private final IInsProjectReagentCodeService iInsProjectReagentCodeService;

    /**
     * 查询产品试剂代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsProjectReagentCode insProjectReagentCode) {
        startPage();
        LambdaQueryWrapper<InsProjectReagentCode> lqw = new LambdaQueryWrapper<InsProjectReagentCode>();
        if (StringUtils.isNotBlank(insProjectReagentCode.getMachineType())) {
            lqw.eq(InsProjectReagentCode::getMachineType, insProjectReagentCode.getMachineType());
        }
        if (insProjectReagentCode.getProjectId() != null) {
            lqw.eq(InsProjectReagentCode::getProjectId, insProjectReagentCode.getProjectId());
        }
        if (StringUtils.isNotBlank(insProjectReagentCode.getReagentType())) {
            lqw.eq(InsProjectReagentCode::getReagentType, insProjectReagentCode.getReagentType());
        }
        if (StringUtils.isNotBlank(insProjectReagentCode.getCodeNo())) {
            lqw.eq(InsProjectReagentCode::getCodeNo, insProjectReagentCode.getCodeNo());
        }
        List<InsProjectReagentCodeDTO> list = iInsProjectReagentCodeService.listProjectReagentCodeDTO(lqw);
        list.forEach(projectReagentCodeDTO -> {
            LambdaQueryWrapper<InsProjectReagentCode> lqw2 = new LambdaQueryWrapper<InsProjectReagentCode>();
            lqw2.eq(InsProjectReagentCode::getMachineType, projectReagentCodeDTO.getMachineType());
            lqw2.eq(InsProjectReagentCode::getProjectId, projectReagentCodeDTO.getProjectId());
            List<InsProjectReagentCode> projectReagentCodeList = iInsProjectReagentCodeService.list(lqw2);
            projectReagentCodeDTO.setProjectReagentCodeList(projectReagentCodeList);
        });
        return getDataTable(list);
    }

    /**
     * 导出产品试剂代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:export')")
    @Log(title = "产品试剂代码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsProjectReagentCode insProjectReagentCode) {
        LambdaQueryWrapper<InsProjectReagentCode> lqw = new LambdaQueryWrapper<InsProjectReagentCode>(insProjectReagentCode);
        List<InsProjectReagentCode> list = iInsProjectReagentCodeService.list(lqw);
        ExcelUtil<InsProjectReagentCode> util = new ExcelUtil<InsProjectReagentCode>(InsProjectReagentCode.class);
        return util.exportExcel(list, "projectReagentCode");
    }

    /**
     * 获取产品试剂代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:query')")
    @GetMapping(value = "/{projectReagentCodeId}")
    public AjaxResult getInfo(@PathVariable("projectReagentCodeId") Integer projectReagentCodeId) {
        return AjaxResult.success(iInsProjectReagentCodeService.getById(projectReagentCodeId));
    }

    /**
     * 获取产品试剂代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:query')")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfoDetail(InsProjectReagentCode projectReagentCode) {
        LambdaQueryWrapper<InsProjectReagentCode> lqw = new LambdaQueryWrapper<>();
        lqw.eq(InsProjectReagentCode::getMachineType, projectReagentCode.getMachineType());
        lqw.eq(InsProjectReagentCode::getProjectId, projectReagentCode.getProjectId());

        List<InsProjectReagentCode> list = iInsProjectReagentCodeService.list(lqw);
        InsProjectReagentCodeDTO projectReagentCodeDTO = new InsProjectReagentCodeDTO();
        BeanUtils.copyProperties(list.get(0), projectReagentCodeDTO);
        projectReagentCodeDTO.setProjectReagentCodeList(list);
        return AjaxResult.success(projectReagentCodeDTO);
    }

    /**
     * 新增产品试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:add')")
    @Log(title = "产品试剂代码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsProjectReagentCode insProjectReagentCode) {
        return toAjax(iInsProjectReagentCodeService.save(insProjectReagentCode) ? 1 : 0);
    }

    /**
     * 批量新增产品试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:addBatch')")
    @Log(title = "批量新增产品试剂代码", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatch(@RequestBody ObjInsProjectReagentCodeList objList) {
        return toAjax(iInsProjectReagentCodeService.saveBatch(objList.getList()) ? 1 : 0);
    }

    /**
     * 修改产品试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:edit')")
    @Log(title = "产品试剂代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsProjectReagentCodeDTO insProjectReagentCodeDTO) {
        List<InsProjectReagentCode> projectReagentCodeList = insProjectReagentCodeDTO.getProjectReagentCodeList();
        projectReagentCodeList.forEach(one -> {
            if (one.getProjectReagentCodeId() != null) {
                iInsProjectReagentCodeService.updateById(one);
            } else {
                iInsProjectReagentCodeService.save(one);
            }
        });
        return AjaxResult.success();
    }

    /**
     * 删除产品试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:remove')")
    @Log(title = "产品试剂代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectReagentCodeIds}")
    public AjaxResult remove(@PathVariable Integer[] projectReagentCodeIds) {
        return toAjax(iInsProjectReagentCodeService.removeByIds(Arrays.asList(projectReagentCodeIds)) ? 1 : 0);
    }

    /**
     * 批量删除产品试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectReagentCode:removeBatch')")
    @Log(title = "批量删除产品试剂代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBatch")
    public AjaxResult removeBatch(@RequestBody ObjInsProjectReagentCodeList objList) {
        List<InsProjectReagentCode> list = objList.getList();
        list.forEach(projectReagentCode -> {
            LambdaQueryWrapper<InsProjectReagentCode> lqw = new LambdaQueryWrapper<>();
            lqw.eq(InsProjectReagentCode::getMachineType, projectReagentCode.getMachineType());
            lqw.eq(InsProjectReagentCode::getProjectId, projectReagentCode.getProjectId());
            iInsProjectReagentCodeService.remove(lqw);
        });
        return AjaxResult.success();
    }
}

@Data
class ObjInsProjectReagentCodeList {
    List<InsProjectReagentCode> list;
}